﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EMC.EQUI.Core
{
    /// <summary>
    /// 儲位
    /// </summary>
    public class Location
    {
        /// <summary>
        /// 仓库编号
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "仓库编号", IndexGroupNameList = new string[] { "index_warehouscode" })]
        public string WarehouseCode { get; set; }

        /// <summary>
        /// 库别
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "库别")]
        public string WarehouseType { get; set; }
        /// <summary>
        /// 库区编号
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "库区编号", IndexGroupNameList = new string[] { "index_zonecode" })]
        public string ZoneCode { get; set; }
        /// <summary>
        /// 货位别名 L010502  一排五列二层  (L表示区分所属唯一仓)
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "货位别名", IndexGroupNameList = new string[] { "index_aliascode" })]
        public string AliasCode { get; set; }

        /// <summary>
        /// 排列层
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "排列层", IndexGroupNameList = new string[] { "index_rowcolumnlayer" })]
        public string RowColumnLayer { get; set; }
        /// <summary>
        /// 排
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "排")]
        public int? RowNum { get; set; }
        /// <summary>
        /// 列
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "列")]
        public int? ColumnNum { get; set; }
        /// <summary>
        /// 层
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "层")]
        public int? LayerNum { get; set; }

        /// <summary>
        /// 巷道编号
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "巷道编号")]
        public int? TunnelNum { get; set; }
        /// <summary>
        /// 此库位所在伸位(1:表示立库最外层  2：表示外层里面的一层 3.表示外层里面的一层的里面一层  ...以此类推)
        /// </summary>
        [SugarColumn(ColumnDescription = "此库位所在伸位")]
        public int ExitorOut { get; set; } = 1;

        /// <summary>
        /// 最大伸位   1:：表示单伸位  2：表示双伸位  3 ... 以此类推
        /// </summary>
        [SugarColumn(ColumnDescription = "最大伸位")]
        public int ExitorMax { get; set; } = 1;

        /// <summary>
        /// 货位限重
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "货位限重")]
        public int? LimitWeight { get; set; }
        /// <summary>
        /// 货位限高
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "货位限高")]
        public int? LimitHeight { get; set; }

        /// <summary>
        /// 占位符-箱子、棧板或其它
        /// </summary>
        [SugarColumn(ColumnDescription = "占位符", IndexGroupNameList = new string[] { "index_symbol" })]
        public string? Symbol { get; set; }
        /// <summary>
        /// 分组标识
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "分组标识")]
        public LocationGroupType LocationGroupType { get; set; } = LocationGroupType.Normal;

        [SugarColumn(IsNullable = true, ColumnDescription = "库位坐标")]
        public string Xyz { get; set; }
        /// <summary>
        /// 激活标识
        /// </summary>
        [SugarColumn(ColumnDescription = "激活标识")]
        public bool IsEnabled { get; set; } = true;
        /// <summary>
        /// 可入库标识-  锁定：已经生成任务 可出：表示有箱子  可入：表示无箱子  禁用：表示此库位异常
        /// </summary>
        [SugarColumn(ColumnDescription = "可出入库标识")]
        public IsOutEnterStatus IsOutEnterStatus { get; set; } = IsOutEnterStatus.In;
        /// <summary>
        /// 是否空容器 - true:表示是空箱子  false:表示存入货物的箱子
        /// </summary>
        [SugarColumn(ColumnDescription = "是否空容器")]
        public bool IsNullContainer { get; set; } = false;
        [SugarColumn(ColumnDescription = "备注", Length = 2000)]
        public string? Remark { get; set; }

        public virtual Location Clone()
        {
            //MemberwiseClone：返回当前对象的浅表副本(它是Object对象的基方法)
            return (Location)this.MemberwiseClone();
        }
        /// <summary>
        /// 当前库位需要将哪个库位移出
        /// </summary>
        /// <param name="needMoveRowNums">需要移库排的配置</param>
        /// <returns></returns>
        public List<Location> NeedMoveLocationRowColumnLayer()
        {
            if (IsNeedMove())
            {
                Location moveLocation = new Location();
                //需要将此库位移出
                moveLocation.RowNum = LocationConfig.NeedMoveRowNum[this.RowNum.Value];
                moveLocation.ColumnNum = this.ColumnNum.Value;
                moveLocation.LayerNum = this.LayerNum.Value;
                moveLocation.WarehouseCode = this.WarehouseCode;
                moveLocation.ZoneCode = this.ZoneCode;
                moveLocation.WarehouseType = this.WarehouseType;
                return new List<Location> { moveLocation };
            }
            return new List<Location>();
        }
        /// <summary>
        /// 是否需要移库
        /// </summary>
        /// <param name="needMoveRowNums">需要移库排的配置</param>
        /// <returns></returns>
        public bool IsNeedMove()
        {
            return (CanIn() || CanOut()) && ExitorMax > 1 && LocationConfig.NeedMoveRowNum.Select(r => r.Key).Contains(this.RowNum.Value);
        }
        /// <summary>
        /// 能出库
        /// </summary>
        /// <returns></returns>
        public bool CanOut()
        {
            return this.IsEnabled == true && this.IsOutEnterStatus == IsOutEnterStatus.Out && this.LocationGroupType == LocationGroupType.Normal;
        }
        
        /// <summary>
        /// 能出的空箱
        /// </summary>
        /// <returns></returns>
        public bool CanOutNullContainer()
        {
            return CanOut() && this.IsNullContainer == true;
        }
        /// <summary>
        /// 能入库
        /// </summary>
        /// <returns></returns>
        public bool CanIn()
        {
            return this.IsEnabled == true && this.IsOutEnterStatus == IsOutEnterStatus.In && this.LocationGroupType == LocationGroupType.Normal;
        }
        /// <summary>
        /// 能入的中转储位
        /// </summary>
        /// <returns></returns>
        public bool CanInTransferStation()
        {

            return this.IsEnabled == true && this.IsOutEnterStatus == IsOutEnterStatus.In &&
                    LocationConfig.NeedMoveRowNum.Select(c => c.Value).Contains(this.ExitorOut) &&
                  this.LocationGroupType == LocationGroupType.TransferStation;
        }

        /// <summary>
        /// 能出的中转储位
        /// </summary>
        /// <returns></returns>
        public bool CanOutTransferStation()
        {
            return this.IsEnabled == true && this.IsOutEnterStatus == IsOutEnterStatus.Out &&
                LocationConfig.NeedMoveRowNum.Select(c => c.Value).Contains(this.ExitorOut) &&
                this.LocationGroupType == LocationGroupType.TransferStation;
        }

        /// <summary>
        /// 重置当前库位
        /// </summary>
        public void Reset()
        {
            this.Symbol = string.Empty;
            this.IsOutEnterStatus = IsOutEnterStatus.In;
            this.Remark = string.Empty;
        }
    }

}
